package com.stone;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DB {
	String driver = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/syang?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true";
	String user = "root";
	String passwd = "admin";
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement stm = null;

	public DB() {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	// in order to update and insert operation
	public int update(GameInfo ginfo) {
		String sql = "INSERT INTO `gameinfo` " +
				"(`size`, `downnum`, `sendtime`, `info`, `category`, `versions`, `name`, `author`, `price`, `pictures`, `downurl`,  `srcurl`, `srcwebsite`, `srcwebsitename`)" +
				" VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		
		int result = 0;
		try {
			conn = DriverManager.getConnection(url, user, passwd);
			stm = conn.prepareStatement(sql);
			stm.setString(1, ginfo.getSize());
			stm.setString(2, ginfo.getDownnum());
			stm.setString(3, ginfo.getSendtime());
			stm.setString(4, ginfo.getInfo());
			stm.setString(5, ginfo.getCategory());
			stm.setString(6, ginfo.getVersions());
			stm.setString(7, ginfo.getName());
			stm.setString(8, ginfo.getAuthor());
			stm.setString(9, ginfo.getPrice());
			stm.setString(10, ginfo.getPictures());
			stm.setString(11, ginfo.getDownurl());
			stm.setString(12, ginfo.getSrcurl());
			stm.setString(13, ginfo.getSrcwebsite());
			stm.setString(14, ginfo.getSrcwebsitename());
			
			result = stm.executeUpdate();
		} catch (SQLException e) { 
			e.printStackTrace();
		}
		return result;
	}

	
	
	// in order to close with database
	public void close() {
		try {
			if (rs != null)
				rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}